package java8_16;

import java.util.Scanner;

public class Chlm {
    public static void main(String[] args) {
        final int K = 10;//订餐数目
        final int Dish = 4;//菜品数目
        Scanner sc = new Scanner(System.in);
        String[] names = new String[K];//姓名
        String[] dishMegs = new String[K];//菜品及份数
        String[] dishNames = {"红烧带鱼", "肉末茄子", "毛豆烧鸡", "宫爆鸡丁"};//菜品名称
        double[] prices = new double[]{38.0, 20.0, 10.0, 30.0};//单价
        int[] praiseNums = new int[4];//点赞数？

        int[] times = new int[K];//时间
        String[] addresses = new String[K];//地址
        int[] states = new int[K];//状态：0表示预定，1表示已完成
        double[] sumPrices = new double[K];//总金额

        int num = -1;//输入0返回主菜单，否则退出系统
        boolean b = false;//标志用户是否退出系统：true为退出系统
        System.out.println("\n欢迎使用“吃货联盟订餐系统”");
        do {
            System.out.println("***************************");
            System.out.println("1、我要订餐");
            System.out.println("2、查看餐带");
            System.out.println("3、签收订单");
            System.out.println("4、删除订单");
            System.out.println("5、我要点赞");
            System.out.println("6、退出系统");
            System.out.println("******************************");
            System.out.print("请选择：");
            int choose = sc.nextInt();//记录用户选择的功能编号
            switch (choose) {
                case 1:
                    System.out.println("***我要订餐***");
                    boolean c = false;//记录是否可以订餐
                    for (int i = 0; i < names.length; i++) {
                        if (names[i] == null) {//找到第一个空位置，可以添加订单
                            c = true;//标志位置，可以订餐
                            System.out.print("请输入订餐人姓名：");
                            String name = sc.next();
                            System.out.println("序号" + "\t" + "菜名" + "\t" + "单价" + "\t" + "点赞数");
                            for (int j = 0; j < dishNames.length; j++) {
                                String price = prices[j] + "元";
                                String priaiseNum = (praiseNums[j]) > 0 ? praiseNums[j] + "赞" : "0";
                                System.out.println((j + 1) + "\t" + dishNames[j] + "\t" + price + "\t" + priaiseNum);
                            }//用户点菜
                            System.out.print("请选择您要点的菜品编号：");
                            int chooseDish = sc.nextInt();
                            System.out.println("请选择您要的份数：");
                            int number = sc.nextInt();
                            String dishMeg = dishNames[chooseDish - 1] + "" + number + "份";
                            double sumPrice = prices[chooseDish - 1] * number;
                            double deliCharge = (sumPrice >= 50) ? 0 : 5;//是否满50；满免餐费5元
                            System.out.print("请输入送餐时间(送餐时间为10点至20点间整数点送餐)：");
                            int time = sc.nextInt();
                            while (time < 10 || time > 20) {
                                System.out.print("您的输入有误，请输入10到20之间整数！");
                                time = sc.nextInt();
                            }
                            System.out.print("请输入您的送餐地址：");
                            String address = sc.next();
                            System.out.println("订餐成功！");
                            System.out.println("您订的是：" + dishMeg);
                            System.out.println("送餐时间：" + time + "点");
                            System.out.println("餐费：" + sumPrice + "元，送餐费" + deliCharge + "元，总计：" + (sumPrice + deliCharge) + "元");
                            names[i] = name;
                            dishMegs[i] = dishMeg;
                            times[i] = time;
                            addresses[i] = address;
                            sumPrices[i] = sumPrice + deliCharge;
                            break;
                        }
                    }
                    if (!c) {
                        System.out.println("对不起，您的餐带已满！");
                    }
                    break;
                case 2:
                    System.out.println("***查看餐带***");
                    System.out.println("序号\t订餐人\t餐品信息\t\t送餐时间\t送餐地址\t\t总金额\t订单状态");
                    for (int a = 0; a < names.length; a++) {
                        if (names[a] != null) {
                            String state = (states[a] == 0) ? "已预定" : "已完成";
                            String date = times[a] + "点";
                            String sumPrice = sumPrices[a] + "元";
                            System.out.println((a + 1) + "\t" + names[a] + "\t" + dishMegs[a] + "\t" + date + "\t" + addresses[a] +
                                    "\t" + sumPrice + "\t" + state);
                        }
                    }
                    break;
                case 3:
                    System.out.println("***签收订单***");
                    boolean d = false;
                    System.out.print("请选择要签收的订单序号：");
                    int f = sc.nextInt();//从1开始

                    if (f<=names.length && names[f-1] != null && states[f-1] == 0) {
                        states[f-1] = 1;
                        System.out.println("订单签收成功！");
                        d = true;
                    } else if (names[f-1] != null && states[f-1] == 1 ) {
                        System.out.println("订单已签收，不能再次签收");
                        d = true;
                    }
                    if (!d) {
                        System.out.println("无订单！");
                    }
                    break;
                case 4:
                    System.out.println("***删除订单***");
                    boolean m = false;
                    System.out.print("请输入要删除的订单序号：");
                    int u = sc.nextInt();
                    //不需要循环
                    for (int i = 0; i < names.length; i++) {
                        if (names[i] != null && states[i] == 1 && u == i + 1) {
                            m = true;
                            for (int j = u - 1; j < names.length - 1; j++) {
                                names[j] = names[j + 1];
                                dishMegs[j] = dishMegs[j + 1];
                                times[j] = times[j + 1];
                                addresses[j] = addresses[j + 1];
                                states[j] = states[j + 1];
                                sumPrices[j] = sumPrices[j + 1];
                                //最后一位清空
                            }
                            int a = names.length - 1;
                            names[a] = null;
                            dishMegs[a] = null;
                            times[a] = 0;
                            addresses[a] = null;
                            states[a] = 0;
                            sumPrices[a] = 0;
                            System.out.println("删除成功！");
                            break;
                        } else if (names[i] != null && states[i] == 0 && u == i + 1) {
                            System.out.println("您选择的为没有签收，不可删除！");
                            m = true;
                            break;
                        }
                    }
                    if (!m) {
                        System.out.println("您要删除的不存在！");
                    }
                    break;
                case 5:
                    System.out.println("***我要点赞***");
                    System.out.println("序号" + "\t" + "菜名" + "\t" + "单价");
                    for (int z = 0; z < dishNames.length; z++) {
                        String price = prices[z] + "元";
                        String priaiseNum = (praiseNums[z]) > 0 ? praiseNums[z] + "赞" : "";
                        System.out.println((z + 1) + "\t" + dishNames[z] + "\t" + price + "\t" + priaiseNum);
                    }
                    System.out.print("请选择您要点赞的菜品序号：");
                    int w = sc.nextInt();
                    //w是否会越界？
                    praiseNums[w - 1]++;
                    System.out.println("点赞成功！");
                    break;
                case 6:
                    b = true;
                    break;
                default:
                    b = true;
                    break;
            }
            if (!b) {
                System.out.print("输入0返回：");
                num = sc.nextInt();
            } else {
                break;
            }
        } while (num == 0);
    }
}
